<template>

    <a-checkbox-group v-model="checkedRoles" class="userRoleEditRoot">
        <a-row >
            <template v-for="item in roleData" >
                <a-col :span="12" style="margin-top: 10px"><a-checkbox :value="item.roleId">{{item.roleName}}</a-checkbox></a-col>
            </template>
        </a-row>

    </a-checkbox-group>

</template>

<script>


    import {Radio,Row,Col,Checkbox,message} from "ant-design-vue";
    export default {
        name: "UserRoleEdit",
        props:['value'],
        components: {
            ARadioGroup:Radio.Group,
            ACheckbox:Checkbox,
            ACheckboxGroup:Checkbox.Group,
            ARow:Row,
            ACol:Col
        },
        data(){

            return {
                checkedRoles:[],
                roleData:[]
            };
        },
        mounted(){

            this.getRoleList();
        },
        methods:{

            submitData(){

                this.$axios.put("/auth/user/updateUserRole?userId=" + this.value,this.checkedRoles)
                    .then(d=>{
                        if(d.data.status == 0){
                            message.success("更新成功");
                            this.$emit("ok");
                        }else{
                            message.error(d.data.message);
                        }
                    }).catch(e=>{
                        message.error("更新失败");

                })
            },

            getRoleList(){
                this.$axios.get("/auth/role/getRoleList")
                    .then(d=>{
                        let result = d.data;
                        if(result.status == 0){

                            this.roleData = result.data;

                            this.getUserRole();
                        }
                    })
                    .catch(e=>{
                        message.error("获取全部角色信息错误");
                    })
            },

            getUserRole(){
                this.$axios.get("/auth/user/getUserRole?userId=" + this.value)
                    .then(d=>{

                        if(d.data.status == 0){

                            let rdata = d.data.data;

                            let cdata = [];
                            for(let r of rdata){
                                cdata.push(r.roleId);
                            }
                            this.checkedRoles = cdata;

                        }

                    })
                    .catch(e=>{
                        message.error("获取用户角色信息错误");
                    })

            }

        },
        watch:{
            value(){
                this.getUserRole();
            }
        }
    }
</script>

<style scoped>

    .userRoleEditRoot{

    }

</style>